﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.ComponentModel;

#endregion

namespace InterLinq.Test.Views {
	/// <summary>
	/// A view that contains per order position is sum
	/// </summary>
	public class SumByOrderPositionsView : INotifyPropertyChanged {
		#region Public Methods

		public SumByOrderPositionsView(IOrderPosition orderPosition, Func<IOrderPosition, decimal> calculateSummandMethod) {
			m_orderPosition = orderPosition;
			m_calculateCountMethod = calculateSummandMethod;
			if (orderPosition as WPFOrderPosition != null) {
				(orderPosition as WPFOrderPosition).CountChanged += CountHasChanged;
			}
			if (orderPosition.Product as WPFProduct != null) {
				(orderPosition.Product as WPFProduct).NameChanged += NameHasChanged;
			}
		}

		#endregion

		#region Private Fields

		private readonly Func<IOrderPosition, decimal> m_calculateCountMethod;
		public IOrderPosition m_orderPosition;

		#endregion

		public string ProductName {
			get { return m_orderPosition.Product.Name; }
			set { m_orderPosition.Product.Name = value; }
		}

		public decimal TotalPrice {
			get { return m_calculateCountMethod(m_orderPosition); }
		}

		public decimal Price {
			get { return m_orderPosition.Product.Price; }
		}

		public int Count {
			get { return m_orderPosition.Count; }
			set { m_orderPosition.Count = value; }
		}

		#region private methods

		private void CountHasChanged(object sender, EventArgs e) {
			if (PropertyChanged != null) {
				PropertyChanged(this, new PropertyChangedEventArgs("TotalPrice"));
			}
		}

		private void NameHasChanged(object sender, EventArgs e) {
			if (PropertyChanged != null) {
				PropertyChanged(this, new PropertyChangedEventArgs("ProductName"));
			}
		}

		#endregion

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion
	}
}